import config from "./config.js";

const BASE_URL = config.apiUrl;

const baseRequest = function (api, methods, options) {
  const header = {
    "Content-Type": "application/json; charset=UTF-8",
  };

  if (options.needToken) {
    header["token"] = uni.getStorageSync("token");
  }

  // if (methods == 'get') {
  // 	api = to_get_string(options.params, api)
  // 	header['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
  // 	options.params = {}
  // }
  // console.log(options, api, header, 88888888);
  return new Promise((resolve, reject) => {
    uni.request({
      url: BASE_URL + api,
      method: methods,
      data: options.params,
      header: Object.assign(header, options.header),
      sslVerify: false,
      success(res) {
        if (res.errMsg === "request:ok") {
          // console.log(res.data);
          if (res.data.code == 1) {
            // console.log(res.data);
            resolve(res.data);
          } else if (res.data.code == -1) {
            uni.showModal({
              content: res.data.msg,
              confirmText: "重新登录",
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  uni.reLaunch({
                    url: "/pages/login/login",
                  });
                  uni.clearStorageSync();
                }
              },
            });
          } else if (res.data.code == 500) {
            uni.showToast({
              title: res.data.msg,
              icon: "none",
            });
          } else {
            console.log({
              data: res.data,
              msg: res.data.msg,
              api,
            });
            resolve({
              data: res.data,
              msg: res.data.msg,
            });
          }
        } else {
          reject(res.data.info || "系统错误");
        }
      },
      fail(msg) {
        console.log(msg, "0999999");
        let data = {
          mag: "请求失败",
          status: 1, //1没网
        };
        reject(data);
      },
    });
  });
};

const request = {};

["options", "get", "post", "put", "head", "delete", "trace", "connect"].forEach(
  (item) => {
    request[item] = (api, params, options) =>
      baseRequest(api, item, {
        params,
        needToken: true,
        ...options,
      });
  }
);

request.postFile = function (api, params) {
  let data = params;
  let filePath = params.filePath;
  delete data.filePath;
  let formData = data;
  console.log(formData);

  return new Promise((resolve, reject) => {
    uni.uploadFile({
      header: {
        "Content-Type": "multipart/form-data",
        token: uni.getStorageSync("token"),
      },
      url: BASE_URL + api,
      filePath: filePath,
      name: "file",
      formData: formData,
      success: (uploadFileRes) => {
        console.log("上传成功", uploadFileRes.data);
        resolve(JSON.parse(uploadFileRes.data));
      },
      fail: (error) => {
        console.error("上传失败", error);
        resolve(error);
      },
    });
  });
};

export default request;
